<?php
class Backend_ThemesController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * List themes
	 */
	public function indexAction()
	{
		// db
		$layouts	= new Layouts();
		
		// query
		$getLayouts		= $layouts->fetchAll();
		
		// view
		$this->view->count		= $getLayouts->count();
		$this->view->results	= $getLayouts->toArray();
	}
	
	
	public function addAction()
	{
		// vars
		$postData	= $this->_request->getPost();
		
		// form
		$form		= new LayoutsForm();
		
		// db
		$layouts	= new Layouts();
		
		// get post  data
		if($this->_request->isPost() && $form->isValid($postData))
		{
			$form->removeElement('submit');
			$layouts->insert($form->getValues());
			
			$this->_redirect('/backend/themes.html');
		}
		
		// view
		$this->view->form = $form;
	}
	
	
	
	public function editAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);

		if($uid > 0)
		{
			// vars
			$postData	= $this->_request->getPost();
			
			// form
			$form		= new LayoutsForm();
			
			// db
			$layouts	= new Layouts();
			
			// query
			$getLayout	= $layouts->find($uid);
			
			if($getLayout->count() > 0)
			{
				$res = $getLayout->toArray();
				
				if($this->_request->isPost() && $form->isValid($postData))
				{
					$form->removeElement('submit');
					$layouts->update($form->getValues(), 'uid=' . $uid);
					
					$this->_redirect('/backend/themes.html');
				}
				else
				{
					$form->populate($res[0]);
				}
			}
			else
			{
				$this->_redirect('/backend/themes.html');
			}
			
			// view
			$this->view->form = $form;
		}
		else
		{
			$this->_redirect('/backend/themes.html');
		}
	}
	
	
	
	public function deleteAction()
	{
		// params
		$uid = $this->_request->getParam('uid', 0);

		if($uid > 0)
		{
			// vars
			$postData	= $this->_request->getPost();
			
			// db
			$layouts	= new Layouts();
			
			// query
			$getLayout	= $layouts->find($uid);
			
			if($getLayout->count() > 0)
			{
				$res = $getLayout->toArray();
				
				// view
				$this->view->count 		= $getLayout->count();
				$this->view->result		= $res[0];
				
				if($this->_request->isPost() && $res[0]['key'] != 'default')
				{
					$del	= $this->_request->getPost('del');
					
					if ($del == Zend_Registry::get('lang')->_('admin_layouts_delete_yes')) 
					{	
						$layouts->delete('uid=' . $uid);
						$this->_redirect('/backend/themes.html');
					}
					else
					{
						$this->_redirect('/backend/themes.html');
					}
				}
			}
			else
			{
				$this->_redirect('/backend/themes.html');
			}
		}
		else
		{
			$this->_redirect('/backend/themes.html');
		}
	}
}
?>